package com.bw.cashier.page

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.cashier.R
import com.bw.cashier.databinding.ActivityAddChargeBinding
import com.bw.cashier.entity.AddChargeEntity
import com.bw.cashier.vm.CashierViewModel
import com.bw.common.event.CustomEvent
import com.bw.common.view.BaseMvvmActivity
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus

/**
 * 新增收费页面
 */
@Route(path = RouterPath.PATH_ADD_CHARGE_PATH)
@AndroidEntryPoint
class AddChargeActivity : BaseMvvmActivity<ActivityAddChargeBinding>() {

    private val mViewModel :CashierViewModel by viewModels()
    override fun getLayoutId(): Int =R.layout.activity_add_charge
    var address = ""
    var num = 0
    override fun initViews() {
        super.initViews()
        mDataBinding.headerBack.setOnClickListener { finish() }
        mDataBinding.addAddress.text = address
        mDataBinding.payTypeSelect.setOnClickListener {
            //类型选择器
            val typeItems = arrayOf("水电费", "物业费", "报修费", "其他") // 工单类型选项
            //初始化PickerView的构建
            val pvOptions = OptionsPickerBuilder(this){po1,_,_,_ ->
                address = typeItems[po1]
                mDataBinding.payType.setText(typeItems[po1])
                mDataBinding.addAddress.text = address
            }.setSubmitText("确定")
                .setCancelText("取消")
                .setTitleText("选择工单类型")
                .build<String>()
            pvOptions.setPicker(typeItems.toList())
            pvOptions.show()
        }



        //时间选择器
        mDataBinding.startTimeSelect.setOnClickListener {
            val dataPicker = DatePicker(this)
            dataPicker.setTitle("请选择时间")
//            dataPicker.wheelLayout.setRange(DateEntity.target(1900,1,1),DateEntity.today())
            dataPicker.setOnDatePickedListener { year, month, day ->
                val date = "${year}-${month}-${day}"
                mDataBinding.paysStartTime.setText(date)
            }
            dataPicker.show()
        }
        mDataBinding.endTimeSelect.setOnClickListener {
            val dataPicker = DatePicker(this)
            dataPicker.setTitle("请选择时间")
//            dataPicker.wheelLayout.setRange(DateEntity.target(1900,1,1),DateEntity.today())
            dataPicker.setOnDatePickedListener { year, month, day ->
                val date = "${year}-${month}-${day}"
                mDataBinding.paysEndTime.setText(date)
            }
            dataPicker.show()
        }
        //监听事件
        mDataBinding.paysCount.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                if (mDataBinding.paysCount.text.toString()!=null){
                    num = mDataBinding.paysCount.text.toString().toInt() * 20
                    mDataBinding.paysMoney.text = "${num}"
                }else{
                    ToastUtils.showLong("空")
                    return
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })



    }

    override fun loadData() {
        super.loadData()
        mDataBinding.submit.setOnClickListener {



            mViewModel.addChargeData(addChargeEntity = AddChargeEntity(
                actualAmount = num,
                balanceDeduction = 0,
                chargeAmount = num,
                chargeType = mDataBinding.payType.text.toString(),
                contactNumber = "13122220123",
                discountAmount = 0,
                effectiveDate = mDataBinding.paysStartTime.text.toString(),
                expirationDate = mDataBinding.paysEndTime.text.toString(),
                id = 0,
                operator = "张三",
                payerId = 879,
                payerName = "赵凯",
                paymentMethod = "微信",
                paymentStatus = "未缴费",
                paymentTime = mDataBinding.paysStartTime.text.toString(),
                penalty = 0,
                receivableAmount = num
            ))
        }

    }


    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mAddChargeSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            EventBus.getDefault().post(CustomEvent(RouterPath.PATH_CASHIER_DETAIL_PATH,1,1))
            finish()
        }
    }



}